package com.atguigu.atcrowdfunding.service.impl;

import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TAdminExample;
import com.atguigu.atcrowdfunding.exception.LoginException;
import com.atguigu.atcrowdfunding.mapper.TAdminMapper;
import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.util.AppDateUtils;
import com.atguigu.atcrowdfunding.util.Const;
import com.atguigu.atcrowdfunding.util.MD5Util;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class TAdminServiceImpl implements TAdminService {
    @Autowired
    TAdminMapper adminMapper;


    public TAdmin getTAdminByLogin(Map<String, Object> paramMap) {
        //1.密码加密
        //2.查询用户
        String loginacct = (String) paramMap.get("loginacct");
        String userpswd = (String) paramMap.get("userpswd");
        //3.判断用户是否为空
        TAdminExample example = new TAdminExample();
        example.createCriteria().andLoginacctEqualTo(loginacct);
        List<TAdmin> list = adminMapper.selectByExample(example);

        //顺序结构
        //判断有没有这个用户
        if(list==null || list.size()==0){

            throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
        }
        TAdmin admin=list.get(0);
        //判断密码是否一致
        if(!admin.getUserpswd().equals(userpswd)){
            throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
        }
        //返回结果
        return admin;


        //分支结构
//        if (list != null && list.size() == 1) {
//            TAdmin admin = list.get(0);
//            //4.判断密码是否一致
//            if (admin.getUserpswd().equals(userpswd)) {;
//            //5.返回结果
//            return admin;
//        } else {
//                throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
//
//        }
//    }else {
//
//       throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
//        }
//
    }

    public PageInfo<TAdmin> listAdminPage(Map<String, Object> paramMap) {
        TAdminExample example=new TAdminExample();
        String condition = (String)paramMap.get("condition");
        if(!"".equals(condition)){
            example.createCriteria().andLoginacctLike("%"+condition+"%"); //进行模糊查询
            TAdminExample.Criteria criteria2 = example.createCriteria();
            criteria2.andUsernameLike("%"+condition+"%");
            TAdminExample.Criteria criteria3 = example.createCriteria();
            criteria3.andEmailLike("%"+condition+"%");    //用户名、登录名称、或者emial进行有条件的模糊查询
            example.or(criteria2);
            example.or(criteria3);
        }
        example.setOrderByClause("createtime desc");
        List<TAdmin> list = adminMapper.selectByExample(example);
        PageInfo<TAdmin> page=new PageInfo<TAdmin>(list,5);  //navigatePage表示有几个导航页,给属性赋值的过程就叫封装
        return page;
    }

    @Override
    public void saveTAdmin(TAdmin admin) {
        admin.setUserpswd(MD5Util.digest(Const.DEFAULT_USERPSWD));
        admin.setCreatetime(AppDateUtils.getFormatTime());
        //insert into t_admin(loginacct,username,email)  value(?,?,?);
        adminMapper.insertSelective(admin);  //动态sql，有选择性保存
    }

    @Override
    public TAdmin getTAdminById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);  //通过主键id进行查询
    }

    @Override
    public int updateTAdmin(TAdmin admin) {
        return adminMapper.updateByPrimaryKeySelective(admin); //
    }

    @Override
    public void deleteTAdmin(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(List<Integer> idList) {
        adminMapper.deleteBatch(idList);

    }
}
